package com.lu.design.pattern.creational.singleton;

import java.io.*;
import java.lang.reflect.Constructor;

public class Test {

    public static void main(String[] args) {
        //LazySingleton instance = LazySingleton.getInstance();
        //System.out.println(instance);


//        Thread thread1 = new Thread(new T());
//        Thread thread2 = new Thread(new T());
//
//        thread1.start();
//        thread2.start();

//        HungrySingleton instance = HungrySingleton.getInstance();
//        try {
//            ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream(new File("singleton_file")));
//            objectOutputStream.writeObject(instance);
//
//            File file = new File("singleton_file");
//            ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream(file));
//            HungrySingleton newInstance = (HungrySingleton) objectInputStream.readObject();
//
//            System.out.println(instance);
//            System.out.println(newInstance);
//            System.out.println(instance == newInstance);
//
//        } catch (IOException e) {
//            e.printStackTrace();
//        } catch (ClassNotFoundException e) {
//            e.printStackTrace();
//        }

        EnumInstance instance = EnumInstance.getInstance();
        instance.setData(new Object());
        try {
            ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream(new File("singleton_file")));
            objectOutputStream.writeObject(instance);

            File file = new File("singleton_file");
            ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream(file));
            EnumInstance newInstance = (EnumInstance) objectInputStream.readObject();

            System.out.println(instance.getData());
            System.out.println(newInstance.getData());
            System.out.println(instance.getData() == newInstance.getData());

            Class<EnumInstance> objectClass = EnumInstance.class;
            Constructor<EnumInstance> constructor = objectClass.getDeclaredConstructor(null);
            constructor.setAccessible(true);

        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }


        System.out.println("program end");
    }
}
